package com.fruit.controller;

import java.util.ArrayList;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.Application;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.constant.EjdConstants;

 public class SessionRemoveControl implements PhaseListener {
	 
	 private List<String> removeSessionKeys = new ArrayList<String>();
	 Map<String,List<String>>nowPageCanNotRemoveKeys = new HashMap<String,List<String>>();
	 public PhaseId getPhaseId() {
		 return PhaseId.RESTORE_VIEW;
	 }

	 public void beforePhase(PhaseEvent event) {
		 FacesContext fc = SpringFacesUtil.getFacesContext();
		 ExternalContext  externalContext = fc.getExternalContext();
		 Map paramMap = externalContext.getInitParameterMap();
		 removeSessionKeys.clear();
		 nowPageCanNotRemoveKeys.clear();
		 if (null != paramMap) {
			 // for removeSessionKeys start
			 String removeBeanName =(String) paramMap.get("needRemoveSessionBean");
			 String[] removeList = removeBeanName.split(",");
			 if (null != removeList && removeList.length > 0) {
				 for (int i=0; i < removeList.length; i ++) {
					 removeSessionKeys.add(removeList[i]);
				 }
			 }
			// for removeSessionKeys end;
			 // for nowPageCanNotRemoveKeys start
			 String canNotRemoveBeanName = (String)paramMap.get("nowPageCanNotRemoveSessionBean");
			 String[] notRemoveList = canNotRemoveBeanName.split(",");
			 if (null != notRemoveList && notRemoveList.length > 0) {
				 for (int j = 0; j < notRemoveList.length; j ++ ) {
					 String  pageNotRemoveStr = notRemoveList[j];
					 String[] notValues = pageNotRemoveStr.split("-");
					 String key = new String();
					 List<String> values = new ArrayList<String>();
					 for (int k =0 ; k < notValues.length; k ++) {
						 
						 if (k == 0) {
							 key = notValues[k];
						 } else {
							 values.add(notValues[k]);
						 }
						 
					 }
					 nowPageCanNotRemoveKeys.put(key, values);
				 }
				 
			 }
			 // for nowPageCanNotRemoveKeys end
		 }
		 

	 }

	 public void afterPhase(PhaseEvent event) {
		 FacesContext fc = SpringFacesUtil.getFacesContext();
		 HttpServletRequest hRequest = (HttpServletRequest) fc.getExternalContext().getRequest();
		 HttpSession session = hRequest.getSession();
		 String requestUrl = hRequest.getRequestURL().toString();
		 int lastPosition = requestUrl.lastIndexOf("/");
		 String urlLast = requestUrl.substring(lastPosition+1, requestUrl.length());
		  // delete useless sessions when change page
		 List<String> nowPageNotRemoveBean =  nowPageCanNotRemoveKeys.get(urlLast);
		 
		  if (null == nowPageNotRemoveBean || nowPageNotRemoveBean.isEmpty()) {
			  Enumeration<String> en = session.getAttributeNames();
			  while (en.hasMoreElements()) {
				  String key = en.nextElement();
				  if (removeSessionKeys.contains(key)) {
					  session.removeAttribute(key);
				  }
			  }
		  } else {
			  Enumeration<String> en = session.getAttributeNames();
			  while (en.hasMoreElements()) {
				  String key = en.nextElement();
				  if ((removeSessionKeys.contains(key)) && (!nowPageNotRemoveBean.contains(key))) {
					  session.removeAttribute(key);
				  }
			  }
		  }
		  Application application = fc.getApplication();  
		  ViewHandler viewHandler = application.getViewHandler();
		  viewHandler.restoreView(fc, fc.getViewRoot().getViewId()); 
		 
	 }


 } 
